// pages/myPintuan/myPintuan.js
let url_js = require("../../utils/url.js");
let common_js = require("../../utils/common.js");
let const_js = require("../../utils/const.js");
let user_js = require("../../utils/user.js");
let sys_js = require("../../utils/system.js");
let ubt_js = require("../../utils/ubt.js");
let name_js = require("../../utils/ubtButtonName.js");
let api = require("../../utils/apiRequest.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sysInfo: null,
    userInfo: null,
    noPinTuanUrl: url_js.imgUrl + "no_pintuan.png",
    contentStatus: -1,
    moveLineStartLeft: 0,
    animationData: {},
    segSelectedIndex: 0,
    animation: null,
    statusList: [0, 1, 2], //0未成团1拼团成功2拼团失败
    selctedTuanInfo: {
      pageInfo: {
        curPage: 1,
        pageSize: 10,
        noMore: false
      },
      itemList: []
    },
    tuanList: [], //存放所有的状态的订单
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);
    //0.禁止转发
    wx.hideShareMenu();
    //1.初始化数据
    this.initData(options);
    //2.获取屏幕信息
    this.getSystemInfo();
    //3.请求数据
    this.requestToGetUserGroup(this.data.selctedTuanInfo.pageInfo.curPage, this.data.selctedTuanInfo.pageInfo.pageSize, this.data.statusList[this.data.segSelectedIndex]);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },

  //初始化数据
  initData: function(op) {
    //1.更新userInfo
    let tempUserInfo = user_js.userInfo();
    this.setData({
      userInfo: tempUserInfo,
      sysInfo: sys_js.systemInfo()
    });
  },

  //获取屏幕信息
  getSystemInfo: function() {
    if (this.data.sysInfo) {
      let moveLineL = this.data.sysInfo.screenWidth / 6.0 - 24;
      this.setData({
        moveLineStartLeft: moveLineL,
      });
    } else {
      wx.getSystemInfo({
        success: res => {
          //处理滚动条的位置
          let systemInfo = res;
          let moveLineL = systemInfo.screenWidth / 6.0 - 24;
          this.setData({
            moveLineStartLeft: moveLineL,
            sysInfo: systemInfo
          });
        }
      })
    }
  },

  //请求获取数据
  requestToGetUserGroup: function(curPage, pageSize, status, pullDownRefresh) {
    let data = {
      code: user_js.getCode(),
      curPage: curPage,
      pageSize: pageSize,
      status: status,
      userId: this.data.userInfo.userInfo.id
    }
    api.wxRequest(url_js.groupUserGroupUrl, data).then(res => {
      this.handleUserGroupData(res, curPage, status, pullDownRefresh);
      common_js.hidePageLoading(pullDownRefresh);
    }).catch(err => {
      common_js.hidePageLoading(pullDownRefresh);
      common_js.log(err);
    });
  },

  //处理返回的订单列表数据
  handleUserGroupData: function(res, curPage, status, pullDownRefresh) {
    //设置数据
    let tmpList = res.result;
    tmpList.forEach(item => {
      let status = this.data.statusList[this.data.segSelectedIndex];
      let statusTxt = "组团中";
      if (status == 1) {
        statusTxt = "已成团";
      } else if (status == 2) {
        statusTxt = "成团失败";
      }
      item.status = status;
      item.statusTxt = statusTxt;
    });

    let tmpTuanMap = new Map(this.data.tuanList);
    if (pullDownRefresh) {
      tmpTuanMap.delete(status);
    }

    //重新生成
    let tmpSelectedInfo = {};
    tmpSelectedInfo.pageInfo = {};
    tmpSelectedInfo.pageInfo.curPage = curPage;
    tmpSelectedInfo.pageInfo.pageSize = 10;
    tmpSelectedInfo.pageInfo.noMore = false;
    tmpSelectedInfo.itemList = [];

    if (res.page.pages <= curPage) {
      tmpSelectedInfo.pageInfo.noMore = true;
    } else {
      tmpSelectedInfo.pageInfo.curPage++;
    }

    if (tmpTuanMap.get(status)) {
      tmpSelectedInfo.itemList = tmpTuanMap.get(status).itemList;
    }
    if (tmpList.length > 0) {
      tmpSelectedInfo.itemList = tmpSelectedInfo.itemList.concat(tmpList);
    }

    tmpTuanMap.set(status, tmpSelectedInfo);

    this.setData({
      selctedTuanInfo: tmpSelectedInfo,
      contentStatus: 1,
      tuanList: [...tmpTuanMap]
    });
  },

  //获取请求的pageInfo
  requestPageInfo: function() {
    let tmpPageInfo = {};
    tmpPageInfo.pageInfo = {};
    tmpPageInfo.pageInfo.curPage = 1;
    tmpPageInfo.pageInfo.pageSize = 10;
    tmpPageInfo.pageInfo.noMore = false;
    tmpPageInfo.itemList = [];
    return tmpPageInfo;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if (this.data.contentStatus != 1) {
      wx.stopPullDownRefresh();
      return;
    }

    let tmpStatus = this.data.statusList[this.data.segSelectedIndex];
    let tmpPageInfo = this.requestPageInfo();
    //request
    this.requestToGetUserGroup(tmpPageInfo.pageInfo.curPage, tmpPageInfo.pageInfo.pageSize, tmpStatus, true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.selctedTuanInfo.pageInfo.noMore) return;

    this.requestToGetOrderList(this.data.selctedTuanInfo.pageInfo.curPage, this.data.selctedTuanInfo.pageInfo.pageSize, this.data.statusList[this.data.segSelectedIndex]);
  },


  //拼团类型
  onseg: function(e) {
    let index = parseInt(e.currentTarget.dataset.index);
    this.moveLineWithIndex(index);

    let tmpStatus = this.data.statusList[index];
    let tmpTuanMap = new Map(this.data.tuanList);
    let tuanItem = tmpTuanMap.get(tmpStatus);
    if (tuanItem) {
      this.setData({
        selctedTuanInfo: tuanItem
      });
    } else {
      //
      let tmpPageInfo = this.requestPageInfo();
      //request
      this.requestToGetUserGroup(tmpPageInfo.pageInfo.curPage, tmpPageInfo.pageInfo.pageSize, tmpStatus);
    }

    //回到顶部
    wx.pageScrollTo({
      scrollTop: 0
    });
  },

  moveLineWithIndex: function(index) {
    if (!this.data.animation) {
      let animation = wx.createAnimation({
        timingFunction: 'ease',
      });
      this.setData({
        animation: animation
      });
    }
    let translateX = (this.data.sysInfo.screenWidth / 3.0) * index;
    this.data.animation.translateX(translateX).step();
    this.setData({
      segSelectedIndex: index,
      animationData: this.data.animation.export()
    });
  },

  //点击拼团列表
  onItem: function(e) {
    let index = e.currentTarget.dataset.index;
    let item = this.data.selctedTuanInfo.itemList[index];
    wx.navigateTo({
      url: '../myPintuaDetail/myPintuaDetail?groupId=' + item.groupId
    });
  }
})